package com.amigo.online.provider.sync.convert.impl;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.common.ConstantValue;
import com.amigo.online.provider.sync.abs.AbstractMongoConvertService;
import com.amigo.online.provider.sync.abs.ColumnHandleMap;
import com.amigo.online.provider.sync.convert.ConvertService;

/**
 * @version: V 1.0.0
 * @Description: (MYSQL 和 Mongo 对应user对象实体转换)
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月10日 下午4:14:10
 */
@Service("mongoGoodsEntityConvertServiceImpl")
public class MongoGoodsEntityConvertServiceImpl extends AbstractMongoConvertService implements ConvertService<JSONObject, Document> {

    @Autowired
    ApplicationContext applicationContext;

    /**
     * <p>Title: convert</p>
     * <p>Description: </p>
     *
     * @param s
     * @param tableName
     * @return
     * @see com.amigo.online.provider.sync.convert.ConvertService#convert(java.lang.Object, java.lang.String)
     */
    @Override
    public Document convert(JSONObject source, String tableName) {
        ColumnHandleMap handlerMap = (ColumnHandleMap) applicationContext.getBean(ConstantValue.SOURCE_MONGO + tableName);
        Document goodsDoc = new Document();
        CommonConvert(handlerMap, goodsDoc, source);
        goodsDoc.append("_class", "com.amigo.online.provider.merchant.mongo.entity.GoodsEntity");
        return goodsDoc;
    }




/*	@Override
	public Document convert(JSONObject source) {
		Document userDoc = new Document();		
		Map<String, String> projectMap = basicMongoDataProjectProperties.getUserProjectMap();
		for(String temp : basicMongoDataProjectProperties.getUserUpdateList()) {
			String projectColumn = projectMap.get(temp);
			if(projectColumn != null) {
				userDoc.append(projectColumn, source.get(temp));
			}else {
				userDoc.append(temp, source.get(temp));
			}		
		}
		List<Double> point = new ArrayList<Double>();
		String longitude = source.getString("longitude");
		String lantitude = source.getString("latitude");
		if(longitude != null) {
			point.add(Double.parseDouble(longitude));
		} else {
			point.add(0d);
		}
		if(lantitude != null) {
			point.add(Double.parseDouble(longitude));
		} else {
			point.add(0d);
		}
	
		Document loc = new Document();
		loc.append("type", "Point").append("coordinates", point);
		userDoc.append("loc", loc)
		    .append("loginTime", System.currentTimeMillis()/1000)
		    .append("_class", "com.amigo.online.provider.user.mongo.entity.UserEntity");
		userDoc.append("_id", source.getLongValue("id"))
		.append("nickname", source.getString("nickname"))
		.append("sex", source.getString("sex"))
		.append("avatar", source.getString("avatar"))
		.append("birthday", source.getLongValue("birthday"))
		.append("job", source.getString("job"))
		.append("sign", source.getString("sign"))
		.append("vip", source.getIntValue("vip"))
		.append("celebrity", source.getIntValue("celebrity"))
		.append("verified", source.getIntValue("verified"))		
		.append("loginTime", System.currentTimeMillis()/1000)
		.append("platform", source.getString("platform"))		
		.append("loc", loc)
		.append("online", 1)
		.append("device", source.getString("device"))
		.append("secret", source.getIntValue("secret"))
		.append("account", source.getString("account"))
		.append("openT", source.getInteger("openT"))
		.append("checked", source.getInteger("checked"))
		.append("firstImg", source.getString("firstImg"))
		.append("version", source.getString("version"))
		.append("avatar_small", source.getString("avatar_small"))
				
		return userDoc;
	}*/
}
